/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   fltkimagetool.cpp
 * Author: Administrator
 * 
 * Created on 2021年6月19日, 下午8:10
 */

#include "fltkimagetool.h"
#include <time.h>
#define sorceress_width 75
#define sorceress_height 75
static uchar sorceress_bits[] = {
   0xfc, 0x7e, 0x40, 0x20, 0x90, 0x00, 0x07, 0x80, 0x23, 0x00, 0x00, 0xc6,
   0xc1, 0x41, 0x98, 0xb8, 0x01, 0x07, 0x66, 0x00, 0x15, 0x9f, 0x03, 0x47,
   0x8c, 0xc6, 0xdc, 0x7b, 0xcc, 0x00, 0xb0, 0x71, 0x0e, 0x4d, 0x06, 0x66,
   0x73, 0x8e, 0x8f, 0x01, 0x18, 0xc4, 0x39, 0x4b, 0x02, 0x23, 0x0c, 0x04,
   0x1e, 0x03, 0x0c, 0x08, 0xc7, 0xef, 0x08, 0x30, 0x06, 0x07, 0x1c, 0x02,
   0x06, 0x30, 0x18, 0xae, 0xc8, 0x98, 0x3f, 0x78, 0x20, 0x06, 0x02, 0x20,
   0x60, 0xa0, 0xc4, 0x1d, 0xc0, 0xff, 0x41, 0x04, 0xfa, 0x63, 0x80, 0xa1,
   0xa4, 0x3d, 0x00, 0x84, 0xbf, 0x04, 0x0f, 0x06, 0xfc, 0xa1, 0x34, 0x6b,
   0x01, 0x1c, 0xc9, 0x05, 0x06, 0xc7, 0x06, 0xbe, 0x11, 0x1e, 0x43, 0x30,
   0x91, 0x05, 0xc3, 0x61, 0x02, 0x30, 0x1b, 0x30, 0xcc, 0x20, 0x11, 0x00,
   0xc1, 0x3c, 0x03, 0x20, 0x0a, 0x00, 0xe8, 0x60, 0x21, 0x00, 0x61, 0x1b,
   0xc1, 0x63, 0x08, 0xf0, 0xc6, 0xc7, 0x21, 0x03, 0xf8, 0x08, 0xe1, 0xcf,
   0x0a, 0xfc, 0x4d, 0x99, 0x43, 0x07, 0x3c, 0x0c, 0xf1, 0x9f, 0x0b, 0xfc,
   0x5b, 0x81, 0x47, 0x02, 0x16, 0x04, 0x31, 0x1c, 0x0b, 0x1f, 0x17, 0x89,
   0x4d, 0x06, 0x1a, 0x04, 0x31, 0x38, 0x02, 0x07, 0x56, 0x89, 0x49, 0x04,
   0x0b, 0x04, 0xb1, 0x72, 0x82, 0xa1, 0x54, 0x9a, 0x49, 0x04, 0x1d, 0x66,
   0x50, 0xe7, 0xc2, 0xf0, 0x54, 0x9a, 0x58, 0x04, 0x0d, 0x62, 0xc1, 0x1f,
   0x44, 0xfc, 0x51, 0x90, 0x90, 0x04, 0x86, 0x63, 0xe0, 0x74, 0x04, 0xef,
   0x31, 0x1a, 0x91, 0x00, 0x02, 0xe2, 0xc1, 0xfd, 0x84, 0xf9, 0x30, 0x0a,
   0x91, 0x00, 0x82, 0xa9, 0xc0, 0xb9, 0x84, 0xf9, 0x31, 0x16, 0x81, 0x00,
   0x42, 0xa9, 0xdb, 0x7f, 0x0c, 0xff, 0x1c, 0x16, 0x11, 0x00, 0x02, 0x28,
   0x0b, 0x07, 0x08, 0x60, 0x1c, 0x02, 0x91, 0x00, 0x46, 0x29, 0x0e, 0x00,
   0x00, 0x00, 0x10, 0x16, 0x11, 0x02, 0x06, 0x29, 0x04, 0x00, 0x00, 0x00,
   0x10, 0x16, 0x91, 0x06, 0xa6, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x18, 0x24,
   0x91, 0x04, 0x86, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x18, 0x27, 0x93, 0x04,
   0x96, 0x4a, 0x04, 0x00, 0x00, 0x00, 0x04, 0x02, 0x91, 0x04, 0x86, 0x4a,
   0x0c, 0x00, 0x00, 0x00, 0x1e, 0x23, 0x93, 0x04, 0x56, 0x88, 0x08, 0x00,
   0x00, 0x00, 0x90, 0x21, 0x93, 0x04, 0x52, 0x0a, 0x09, 0x80, 0x01, 0x00,
   0xd0, 0x21, 0x95, 0x04, 0x57, 0x0a, 0x0f, 0x80, 0x27, 0x00, 0xd8, 0x20,
   0x9d, 0x04, 0x5d, 0x08, 0x1c, 0x80, 0x67, 0x00, 0xe4, 0x01, 0x85, 0x04,
   0x79, 0x8a, 0x3f, 0x00, 0x00, 0x00, 0xf4, 0x11, 0x85, 0x06, 0x39, 0x08,
   0x7d, 0x00, 0x00, 0x18, 0xb7, 0x10, 0x81, 0x03, 0x29, 0x12, 0xcb, 0x00,
   0x7e, 0x30, 0x28, 0x00, 0x85, 0x03, 0x29, 0x10, 0xbe, 0x81, 0xff, 0x27,
   0x0c, 0x10, 0x85, 0x03, 0x29, 0x32, 0xfa, 0xc1, 0xff, 0x27, 0x94, 0x11,
   0x85, 0x03, 0x28, 0x20, 0x6c, 0xe1, 0xff, 0x07, 0x0c, 0x01, 0x85, 0x01,
   0x28, 0x62, 0x5c, 0xe3, 0x8f, 0x03, 0x4e, 0x91, 0x80, 0x05, 0x39, 0x40,
   0xf4, 0xc2, 0xff, 0x00, 0x9f, 0x91, 0x84, 0x05, 0x31, 0xc6, 0xe8, 0x07,
   0x7f, 0x80, 0xcd, 0x00, 0xc4, 0x04, 0x31, 0x06, 0xc9, 0x0e, 0x00, 0xc0,
   0x48, 0x88, 0xe0, 0x04, 0x79, 0x04, 0xdb, 0x12, 0x00, 0x30, 0x0c, 0xc8,
   0xe4, 0x04, 0x6d, 0x06, 0xb6, 0x23, 0x00, 0x18, 0x1c, 0xc0, 0x84, 0x04,
   0x25, 0x0c, 0xff, 0xc2, 0x00, 0x4e, 0x06, 0xb0, 0x80, 0x04, 0x3f, 0x8a,
   0xb3, 0x83, 0xff, 0xc3, 0x03, 0x91, 0x84, 0x04, 0x2e, 0xd8, 0x0f, 0x3f,
   0x00, 0x00, 0x5f, 0x83, 0x84, 0x04, 0x2a, 0x70, 0xfd, 0x7f, 0x00, 0x00,
   0xc8, 0xc0, 0x84, 0x04, 0x4b, 0xe2, 0x2f, 0x01, 0x00, 0x08, 0x58, 0x60,
   0x80, 0x04, 0x5b, 0x82, 0xff, 0x01, 0x00, 0x08, 0xd0, 0xa0, 0x84, 0x04,
   0x72, 0x80, 0xe5, 0x00, 0x00, 0x08, 0xd2, 0x20, 0x44, 0x04, 0xca, 0x02,
   0xff, 0x00, 0x00, 0x08, 0xde, 0xa0, 0x44, 0x04, 0x82, 0x02, 0x6d, 0x00,
   0x00, 0x08, 0xf6, 0xb0, 0x40, 0x02, 0x82, 0x07, 0x3f, 0x00, 0x00, 0x08,
   0x44, 0x58, 0x44, 0x02, 0x93, 0x3f, 0x1f, 0x00, 0x00, 0x30, 0x88, 0x4f,
   0x44, 0x03, 0x83, 0x23, 0x3e, 0x00, 0x00, 0x00, 0x18, 0x60, 0xe0, 0x07,
   0xe3, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x70, 0x70, 0xe4, 0x07, 0xc7, 0x1b,
   0xfe, 0x01, 0x00, 0x00, 0xe0, 0x3c, 0xe4, 0x07, 0xc7, 0xe3, 0xfe, 0x1f,
   0x00, 0x00, 0xff, 0x1f, 0xfc, 0x07, 0xc7, 0x03, 0xf8, 0x33, 0x00, 0xc0,
   0xf0, 0x07, 0xff, 0x07, 0x87, 0x02, 0xfc, 0x43, 0x00, 0x60, 0xf0, 0xff,
   0xff, 0x07, 0x8f, 0x06, 0xbe, 0x87, 0x00, 0x30, 0xf8, 0xff, 0xff, 0x07,
   0x8f, 0x14, 0x9c, 0x8f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x9f, 0x8d,
   0x8a, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x07, 0xbf, 0x0b, 0x80, 0x1f,
   0x00, 0x00, 0xff, 0xff, 0xff, 0x07, 0x7f, 0x3a, 0x80, 0x3f, 0x00, 0x80,
   0xff, 0xff, 0xff, 0x07, 0xff, 0x20, 0xc0, 0x3f, 0x00, 0x80, 0xff, 0xff,
   0xff, 0x07, 0xff, 0x01, 0xe0, 0x7f, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x07,
   0xff, 0x0f, 0xf8, 0xff, 0x40, 0xe0, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff,
   0xff, 0xff, 0x40, 0xf0, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff,
   0x41, 0xf0, 0xff, 0xff, 0xff, 0x07};
//按键回掉
static void _reset_callback(Fl_Widget *w, void *data) {
	Fl_File_Input*p=(Fl_File_Input*)data;
	p->value("");	
}
/*
#define FLTK_CALLBACK(x) \
void _##x_callback(Fl_Widget *w, void *data){\
	fltkwin*p=(fltkwin*)data;\
	p->fltk_##x_callback();\
}
FLTK_CALLBACK(openimg)
FLTK_CALLBACK(saveimg)
*/

static void _openimg_callback(Fl_Widget *w, void *data) {
	fltkimagetool*p=(fltkimagetool*)data;
	p->openimg_callback();	
}
static void _saveimg_callback(Fl_Widget *w, void *data) {
	fltkimagetool*p=(fltkimagetool*)data;
	p->saveimg_callback();	
}
fltkimagetool::fltkimagetool()
{
	m_sdlwindow=NULL;
	m_sdlscreen=NULL;
}

fltkimagetool::fltkimagetool(const fltkimagetool& orig)
{
}

fltkimagetool::~fltkimagetool()
{
}
void fltkimagetool::init()
{
	m_window = new Fl_Window(800,600,"图片处理");
	m_input = new Fl_File_Input(40, 10, 480, 40, "File");
	//m_input->callback(_inputfile_callback,(void*)this);
		
	m_resetbutton = new Fl_Button(530, 10, 80, 40, "Reset");
	m_resetbutton->type(FL_NORMAL_BUTTON);
	m_resetbutton->callback(_reset_callback,(void*)m_input);

	m_openimgbutton = new Fl_Button(620, 10, 80, 40, "OpenIMG");
	m_openimgbutton->type(FL_NORMAL_BUTTON);
	m_openimgbutton->callback(_openimg_callback,(void*)this);
	
	m_saveimgbutton = new Fl_Button(710, 10, 80, 40, "SaveIMG");
	m_saveimgbutton->type(FL_NORMAL_BUTTON);
	m_saveimgbutton->callback(_saveimg_callback,(void*)this);
	
	//m_button->callback(fltkwin::xyz_callback,(void*)this);
	//Fl_Bitmap *bmp=new Fl_Bitmap(sorceress_bits,sorceress_width,sorceress_height);
	//m_imgbutton= new Fl_Button (40,60,120,120,"Bitmap");
	//m_imgbutton->image(bmp);
	//跟下方SDL窗口重叠，不播放时使用此窗口
	m_imgbox=new Fl_Box(FL_FLAT_BOX,40,110,720,480,NULL);
	m_window->end();
	//感觉不加没事
	m_window->show(0, NULL);
	//要完成后下面才有HWND
	//用Windows方式创建子控件
	//编译64位的要用GWLP_HINSTANCE，看定义值跟GWL_HINSTANCE一样，在64位中被GWL_HINSTANCE undef
	//貌似返回值一直是0x00400000,实际试验发现HINSTANCE用NULL也行
	HWND tmp_hwnd=CreateWindow (TEXT("STATIC"),"",       
        WS_CHILD | WS_VISIBLE ,40,110,720,480 ,(HWND)fl_xid(m_window), (HMENU) 0,
#if 0		
#ifdef _WIN64
		(HINSTANCE) GetWindowLongPtr ((HWND)fl_xid(m_window), GWLP_HINSTANCE), NULL) ;
#else		
		(HINSTANCE) GetWindowLong ((HWND)fl_xid(m_window), GWL_HINSTANCE), NULL) ;
#endif		
#else		
		NULL,NULL) ;
#endif	
	printf("tmp_hwnd:%p  fltk_hwnd:%p\n",tmp_hwnd,fl_xid(m_window));
	//Fl_JPEG_Image*tmp=new Fl_JPEG_Image("test.jpg");
	//m_imgbox->image(tmp);
	//m_sdlwindow = SDL_CreateWindow("SDL2 Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
	//HWND
	//m_sdlwindow=SDL_CreateWindowFrom((void*)fl_xid(m_window));
	m_sdlwindow=SDL_CreateWindowFrom((void*)tmp_hwnd);
	//SDL_SetWindowPosition(m_sdlwindow,40,110);
	//SDL_SetWindowSize(m_sdlwindow, 720, 480);
	//SDL_ShowWindow(m_sdlwindow);
	SDL_HideWindow(m_sdlwindow);
    m_sdlscreen=SDL_GetWindowSurface(m_sdlwindow);	
}
void fltkimagetool::uninit(){
	if(m_sdlwindow){
		SDL_DestroyWindow(m_sdlwindow);
		m_sdlwindow=NULL;
	}
}

void fltkimagetool::openimg_callback(){
#if 1
	SDL_Surface *surface = IMG_Load(m_input->value());
	if(surface){
		if(surface->format->format!=SDL_PIXELFORMAT_RGB24){
			//SDL_Surface*tmp=SDL_CreateRGBSurfaceWithFormatFrom(surface->pixels,surface->w,surface->h,
			//surface->format->BitsPerPixel,surface->pitch,surface->format->format);
			//SDL_FreeSurface(surface);
			printf("W:%d H:%d pitch:%d bits:%hhu %s\n",surface->w,surface->h,surface->pitch,
			surface->format->BitsPerPixel,SDL_GetPixelFormatName(surface->format->format));
			SDL_Surface*tmp=SDL_ConvertSurfaceFormat(surface,SDL_PIXELFORMAT_RGB24,0);
			SDL_FreeSurface(surface);
			surface=tmp;
			printf("W:%d H:%d pitch:%d bits:%hhu %s\n",surface->w,surface->h,surface->pitch,
			surface->format->BitsPerPixel,SDL_GetPixelFormatName(surface->format->format));
		}
		Fl_RGB_Image*tmp=new Fl_RGB_Image((uchar*)surface->pixels,surface->w,surface->h,
			surface->format->BitsPerPixel/8,surface->pitch);
		//Fl_Bitmap *tmp=new Fl_Bitmap((uchar*)surface->pixels,surface->w,surface->h);
		//Fl_Image* pImg = tmp->copy(720,480);
		int dtsw=m_imgbox->w();
		int dtsh=m_imgbox->h();
		fltk_count_autosize(surface->w,surface->h,&dtsw,&dtsh);
		printf("IMG:%dx%d BOX:%dx%d DST:%dx%d\n",surface->w,surface->h,
			m_imgbox->w(),m_imgbox->h(),dtsw,dtsh);
		Fl_Image* pImg = tmp->copy(dtsw,dtsh);
		m_imgbox->image(pImg);
		m_imgbox->redraw();
		SDL_FreeSurface(surface);
	}
#elif 0
	Fl_Shared_Image*tmp=Fl_Shared_Image::get(m_input->value());	
	if(tmp){
		//不指定大小会异常，还失真
		Fl_Image* pImg = tmp->copy(720,480);
		m_imgbox->image(pImg);
		m_imgbox->redraw();
	}
#else	
	SDL_Surface *image = IMG_Load(m_input->value());
	if(image){
		SDL_BlitSurface(image, NULL, m_sdlscreen, NULL);
		SDL_FreeSurface(image);
		SDL_UpdateWindowSurface(m_sdlwindow);
	}
#endif	
}
void fltkimagetool::saveimg_callback(){
	char filename[1024]={0};
	snprintf(filename,1024,"%ld.jpg",time(0));
	IMG_SaveJPG(m_sdlscreen,filename,100);
}
